package com.cmpanyandi.elearning.wordsdriller.activity;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.AdapterContextMenuInfo;

import com.cmpanyandi.elearning.utils.db.DbAdapter;
import com.cmpanyandi.elearning.wordsdriller.model.Dictionary;
import com.cmpanyandi.elearning.wordsdriller.model.DictionaryItem;

/**
 * This activity is responsible for displaying list of dictionary items.
 * 
 * @author wojtek
 * 
 */
public class DictionaryItemList extends DictionaryActivityList {

    private static final String TAG = "ItemListActivity";
    private Long dictionaryId;
    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.dictionary_item_list);
	dictionaryId = getIntent().getExtras().getLong(DictionaryItem.DICTIONARY);
	Dictionary dict = (Dictionary)DbAdapter.getInstance().getByPK(new Dictionary(dictionaryId));
	setTitle(dict.getName());
	fillData(dictionaryId);
	registerForContextMenu(getListView());

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
	super.onCreateOptionsMenu(menu);
	menu.add(0, ADD, 0, R.string.menu_add_item);
	return true;
    }
   
    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
	switch (item.getItemId()) {
	case ADD:
	    createItem();
	    return true;
	}

	return super.onMenuItemSelected(featureId, item);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
	super.onListItemClick(l, v, position, id);
	Intent i = new Intent(this, DictionaryEdit.class);
	i.putExtra(DictionaryItem.PK, id);
	startActivityForResult(i, ACTIVITY_EDIT);
    }

    private void createItem() {

	Intent i = new Intent(this, DictionaryItemEdit.class);
	i.putExtra(DictionaryItem.DICTIONARY, dictionaryId);
	startActivityForResult(i, ACTIVITY_CREATE);
    }

    private void fillData(long dictionaryId) {
	DictionaryItem item = new DictionaryItem();
	item.setDictionary(new Dictionary(dictionaryId));

	Cursor itemCursor = DbAdapter.getInstance().fetchBeans(item);
	startManagingCursor(itemCursor);

	String[] from = new String[] { DictionaryItem.NAME, DictionaryItem.CONTEXT };

	// and an array of the fields we want to bind those fields to (in this
	// case just text1)
	int[] to = new int[] { R.id.toptext, R.id.bottomtext };

	// Now create a simple cursor adapter and set it to display
	SimpleCursorAdapter items = new SimpleCursorAdapter(this, R.layout.item_row, itemCursor, from, to);
	setListAdapter(items);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
	super.onCreateContextMenu(menu, v, menuInfo);
	menu.add(0, DELETE, 0, R.string.menu_delete_item);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
	switch (item.getItemId()) {
	case DELETE:
	    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
	    DbAdapter.getInstance().delete(new DictionaryItem(info.id));
	    fillData(dictionaryId);
	    return true;
	}
	return super.onContextItemSelected(item);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
	super.onActivityResult(requestCode, resultCode, intent);
	fillData(dictionaryId);
    }
}
